import { onMounted, reactive, ref } from 'vue'
import ApiCron from '~/apis/cron/ApiCron'
import { DialogType } from '~/types/Enum'

export const index = () => {
    const ruleFormRef = ref<any>()

    const conf = reactive<conf>({
        table: {
            expandall: true,
            source: [],
            data: [],
            loading: true,
            page: {
                pageSize: 10,
                pageNum: 1,
                total: 0
            }
        },
        dialog: {
            title: '',
            labelWidth: 100,
            rules: {
                desc: [{ required: true, message: '请输入备注', trigger: 'blur' }],
                cron: [{ required: true, message: '请输入表达式', trigger: 'blur' }],
                funStr: [{ required: true, message: '请输入执行路径', trigger: 'blur' }]
            },
            show: false,
            loading: false,
            type: DialogType.Insert,
            form: {} as any,
            formDefault: {
                desc: '',
                cron: '',
                funStr: '',
                status: false
            },
            remove: {
                show: false,
                item: {} as any
            }
        }
    })

    const dialogShow = async (type: DialogType, item?: any) => {
        const formEl = ruleFormRef.value
        if (formEl) formEl.resetFields()
        conf.dialog.type = type
        if (type === DialogType.Insert) {
            conf.dialog.title = '新增'
            conf.dialog.form = JSONUtil.cp(conf.dialog.formDefault)
        } else if (type === DialogType.Update) {
            conf.dialog.title = '编辑'
            conf.dialog.form = JSONUtil.cp(item)
        }
        conf.dialog.show = true
    }

    const dialogSubmit = async () => {
        const formEl = ruleFormRef.value
        if (!formEl) return
        conf.dialog.loading = true
        await formEl.validate(async (valid: any, fields: any) => {
            if (valid) {
                let form = JSONUtil.cp(conf.dialog.form)
                const res = await ApiCron.save(form)
                if (res.status) {
                    await getTableData()
                    conf.dialog.show = false
                }
            }
            conf.dialog.loading = false
        })
    }

    const dialogSubmitRemove = async (id: string) => {
        const res = await ApiCron.remove(id)
        if (res.status) {
            await getTableData()
        }
    }

    const getTableData = async () => {
        conf.table.loading = true
        const res = await ApiCron.list({
            pageSize: conf.table.page.pageSize,
            pageNum: conf.table.page.pageNum
        })
        if (res.status) {
            conf.table.data = res.data
            conf.table.page.total = res.total
        }
        conf.table.loading = false
    }

    onMounted(async () => {
        await getTableData()
    })

    return { conf, dialogSubmit, dialogShow, ruleFormRef, dialogSubmitRemove, getTableData }
}

type conf = {
    table: {
        expandall: boolean
        source: any[]
        data: any[]
        loading: boolean
        page: {
            pageSize: number
            pageNum: number
            total: number
        }
    }
    dialog: {
        title: string
        labelWidth: number
        rules: any
        type: DialogType
        show: boolean
        loading: boolean
        form: form
        formDefault: form
        remove: {
            show: boolean
            item: form
        }
    }
}

type form = {
    cron: string
    funStr: string
    desc: string
    status: boolean
}
